"""
@author : linrh
@homepage : https://gitee.com/linrh-DUT
@version: 1.0.0
@when : 2023/5/25
@file: db_util.py
"""
import pymongo

def get_db():
    client = pymongo.MongoClient('localhost', 27017)
    db = client['WordPreview']
    return db

def get_collection(db, collection_name):
    return db[collection_name]

def insert_one(collection, data):
    collection.insert_one(data)

def insert_one_duplicated(collection, data):
    for item in collection.find({'word': data['word']}):
        collection.delete_one(item)
    insert_one(collection, data)

def insert_many(collection, data):
    collection.insert_many(data)

def find_one(collection, data):
    return collection.find_one(data)

def find_many(collection, data):
    return collection.find(data)

def find_all(collection, data=None):
    return collection.find(data)

def update_one(collection, filter, data):
    collection.update_one(filter, data)
